package cn.edu.cjw.APMFP.Resource.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import cn.edu.cjw.APMFP.BOMTable.DAO.BOMTableDAO;
import cn.edu.cjw.APMFP.BOMTable.DAO.BOMTableDAOImpl;
import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.ProductionTask.pojo.ProductionTask;
import cn.edu.cjw.APMFP.Resource.Resource;
import cn.edu.cjw.APMFP.Resource.DAO.ResourceDAO;
import cn.edu.cjw.APMFP.Resource.DAO.ResourceDAOImpl;
import cn.edu.cjw.APMFP.user.pojo.User;

public class ResourceServiceImpl implements ResourceService {

	ResourceDAO resourceDAO = new ResourceDAOImpl();
	BOMTableDAO bomDao = new BOMTableDAOImpl();

	public boolean materialIn(BOMTable bom, Long num, User user) throws Exception {

		if (bom.getBtype().equals(BOMTable.TYPE_PRODUCT)) {
			return false;
		} else {

			Resource resource = new Resource(num, user.getAccount());
			resource.ResetBOM(bom);

			return resourceDAO.addAndupdate(resource);
		}

	}

	public boolean materialOut(ProductionTask task, User user) throws Exception {

		boolean flag = false;

		HashMap<String, Long> material = task.getMaterial();

		Set<Entry<String, Long>> entrySet = material.entrySet();

		for (Entry<String, Long> entry : entrySet) {

			Resource resource = new Resource(-entry.getValue(), user.getAccount());
			resource.ResetBOM(bomDao.searchById(entry.getKey()));

			boolean addAndupdate = resourceDAO.addAndupdate(resource);

			if (addAndupdate) {
				flag = addAndupdate;
			} else {
				flag = false;
				break;
			}

		}

		return flag;
	}

	public boolean productIn(ProductionTask task, User user) throws Exception {

		boolean flag = false;

		HashMap<String, Long> product = task.getProduct();

		Set<Entry<String, Long>> entrySet = product.entrySet();

		for (Entry<String, Long> entry : entrySet) {

			Resource resource = new Resource(entry.getValue(), user.getAccount());
			resource.ResetBOM(bomDao.searchById(entry.getKey()));

			boolean addAndupdate = resourceDAO.addAndupdate(resource);

			if (addAndupdate) {
				
				flag = addAndupdate;
				
			} else {
				
				flag = false;
				
				break;
			}

		}

		return flag;
	}

	public boolean productOut(BOMTable bom, Long num, User user) throws Exception {

		Resource resource = new Resource(-num, user.getAccount());
		resource.ResetBOM(bom);

		return resourceDAO.addAndupdate(resource);
	}

	public ArrayList<Resource> showMaterial() throws Exception {
		return resourceDAO.searchByType(BOMTable.TYPE_MATERIAL);
	}

	public ArrayList<Resource> showProduct() throws Exception {
		return resourceDAO.searchByType(BOMTable.TYPE_PRODUCT);
	}

	public ArrayList<Resource> searchResourceByName(String name) throws Exception {
		return resourceDAO.searchByName(name);
	}

	public Resource searchResourceById(String id) throws Exception {
		return resourceDAO.searchById(id);
	}

	public boolean removeResource(String bid) throws Exception {
		return resourceDAO.remove(bid);
	}

}
